package com.yc.yyds.servlet;

import com.google.gson.Gson;
import com.yc.yyds.Bean.Result;
import com.yc.yyds.biz.BizException;
import com.yc.yyds.biz.UserLoginBiz;
import com.yc.yyds.dao.UserLoginDao;
import com.yc.yyds.dao.Userdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "UserIdLoginServlet", value = "/GLYServlet.s")
public class GLYServlet extends HttpServlet {
    UserLoginDao dao = new UserLoginDao();
    Userdao biz=new Userdao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String userzh = request.getParameter("userzh");
        String pwd = request.getParameter("pwd");
        String username = biz.username(userzh).toString();
        HttpSession session = request.getSession();
         session.setAttribute("usernames",username);
        String ucode = request.getParameter("vcode");
        Object vcode = request.getSession().getAttribute("vcode");

        System.out.println("userzh = " + userzh);
        System.out.println("pwd = " + pwd);
        System.out.println("vcode="+vcode);
        Result result;
        Gson gson = new Gson();
        if(vcode.equals(ucode)==false){
            result= new Result(0, "验证码错误", null);
            String json=gson.toJson(result);
            response.getWriter().append(json);
        }else {
            Map<String, Object> map = biz.qureys(userzh);

            System.out.println("freeze1："+map);
            String maps=map.get("freeze").toString();
            System.out.println("freeze2："+maps);
            if(maps.equals("0")){
                result = new Result(0,"账户已被冻结",null);
                String json = gson.toJson(result);
                response.getWriter().append(json);
            }else {
                try {
                    Map<String, Object> user = biz.login(userzh, pwd);
                    String userid = user.get("userid").toString();
                    System.out.println("userid"+userid);
//                    Cookie cookie = new Cookie("yyds_cinema_userid",userid);
//                    response.addCookie(cookie);
                    result = new Result(1,"登录成功",null);
                    String json = gson.toJson(result);
                    response.getWriter().append(json);
                } catch (Exception e) {
                    e.printStackTrace();
                    result = new Result(0,"用户名或密码错误",null);
                    String json = gson.toJson(result);
                    response.getWriter().append(json);
                }
            }

        }

    }
}
